<?php
/***************************************************************************
*                                main.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
if(checkLoginAdmin()) {
	php5Redirect(sefBuild($php5WebPath, 'index.php?o=cpanel&m=main', 0));
	return;
}
switch ($task){
	case 'mainpage':
		showMainpage();
		break;
	case 'login':
		loginSystem();
		showLoginForm();
		break;	
	case 'logout':
		logoutSystem();
		break;	
	default:
		showLoginForm();
		break;
}
function showMainpage(){
	global $smarty, $php5RootAdminPath, $module, $php5Session, $php5TemplateAdminFile, $lang;	
	checkLogin();
	
	$smarty->assign('H_WELCOME_ADDMIN', $lang['H_WELCOME_ADDMIN']);	
	$smarty->assign('N_SELECT_MENU', $lang['N_SELECT_MENU']);
	$smarty->assign('admin_name', $php5Session->getVar("admin_name"));	
	
	$smarty->display(sprintf($php5TemplateAdminFile, 'en', $module, 'main.tpl'));
}
function logoutSystem(){
	global $smarty, $php5DB, $php5WebPathAD, $php5Session, $php5WebPath;
	$php5Session->setVar("admin_id", 0);
	$php5Session->setVar("admin_name", "");
	$php5Session->setVar("admin_email", "");
	$php5Session->setVar("admin_type", "");
	
	php5Redirect(sefBuild($php5WebPath, 'index.php?o=main&m=main', 0));
}
function loginSystem(){
	global $smarty, $php5DB, $php5WebPathAD, $php5Session, $php5WebPath;
	global $msgAlert, $redirect;
	if ($php5Session->getVar("admin_id")){
		return;
	}
	$email 		= trim(php5GetParam($_REQUEST, 'email', ''));
	$password 		= trim(php5GetParam($_REQUEST, 'pwd', ''));
	$params = array(
		'Email' => $email,
		'Passwd' => $password,
		'accountType' => 'GOOGLE',
		'service' => 'adwords',
		'source' => 'AdWords API PHP Code Example');
	
	$url = 'https://www.google.com/accounts/ClientLogin';
	
	// Make request
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$response = curl_exec($ch);
	curl_close($ch);
	
	// Parse response.
	$lines = explode("\n", $response);
	foreach ($lines as $line) {
	  $parts = explode('=', $line, 2);
	  if ($parts[0] == 'Auth') {
		$authToken = $parts[1];
	  }
	  if ($parts[0] == 'Error') {
		$error = $parts[1];
	  }
	}
	
	// Display results.
	if (isset($authToken)) {
		$php5Session->setVar("admin_id", 1);
		$php5Session->setVar("admin_email", $email);
		$php5Session->setVar("authToken", $authToken);
		php5Redirect(sefBuild($php5WebPath, 'index.php?o=cpanel&m=main', 0));
	} else if (isset($error)) {
		$msgAlert = 'Username and password do not match or you do not have an account yet.';
		$smarty->assign('msgAlert', $msgAlert);
	}	

}
function showLoginForm(){	
	global $smarty, $php5RootAdminPath, $module, $php5Session, $php5WebPath, $php5TemplateAdminFile, $php5RootAdminTempalteCPath, $language;	
	$smarty->setCompileDir(sprintf($php5RootAdminTempalteCPath, 'main_login'));
	$tpl = sprintf($php5TemplateAdminFile, $language, 'main_login', 'main.tpl');
	if(!is_file($tpl )) {
		die("can not find template");
	}
	//error_reporting(E_ALL);
	//ini_set('display_errors','On');
	$smarty->assign('msgAlert', '');
	$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=main_login', 0));
	$smarty->assign('main', $smarty->fetch($tpl));
}


